/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.score.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.score.entity.ExchangeRecord;
import com.boxin.ims.modules.wechat.score.entity.ScoreExchange;
import com.boxin.ims.modules.wechat.score.service.ExchangeRecordService;
import com.boxin.ims.modules.wechat.score.service.ScoreExchangeService;

/**
 * 积分对换记录Controller
 * @author Jakemanse
 * @version 2013-09-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/score/exchangeRecord")
public class ExchangeRecordController extends BaseController {

	@Autowired
	private ExchangeRecordService exchangeRecordService;
	
	@Autowired
	private ScoreExchangeService scoreExchangeService;
	
	
	@ModelAttribute
	public ExchangeRecord get(@RequestParam(required=false) Long id) {
		if (id != null){
			return exchangeRecordService.get(id);
		}else{
			return new ExchangeRecord();
		}
	}
	
	@RequiresPermissions("score:exchangeRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(ExchangeRecord exchangeRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		//是否可以查询微购物的商品       0  为显示       1为不显示
		String flag="0";
		if (!user.isAdmin()){
			exchangeRecord.setUser(user);
			Long count=   scoreExchangeService.getCount(user.getId());
			if(count==0){
				flag="1";
			}
		}
		
		if(exchangeRecord.getStartTime() == null){
			Date now = DateUtils.getDayStartTime();
			exchangeRecord.setCreateTime(now);
			exchangeRecord.setStartTime(now);
		}else{
			exchangeRecord.setCreateTime(exchangeRecord.getStartTime());
		}
		
		
        Page<ExchangeRecord> page = exchangeRecordService.find(new Page<ExchangeRecord>(request, response), exchangeRecord); 
        model.addAttribute("flag", flag);
        model.addAttribute("page", page);
        model.addAttribute("exchangeRecord", exchangeRecord);
		return "modules/wechat/score/exchangeRecordList";
	}

	@RequiresPermissions("score:exchangeRecord:view")
	@RequestMapping(value = "form")
	public String form(ExchangeRecord exchangeRecord, Model model) {
		model.addAttribute("exchangeRecord", exchangeRecord);
		return "modules/wechat/score/exchangeRecordForm";
	}

	@RequiresPermissions("score:exchangeRecord:edit")
	@RequestMapping(value = "save")
	public String save(ExchangeRecord exchangeRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, exchangeRecord)){
			return form(exchangeRecord, model);
		}
		exchangeRecordService.save(exchangeRecord);
		addMessage(redirectAttributes, "保存积分对换记录'" + exchangeRecord.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/score/exchangeRecord/?repage";
	}
	
	@RequiresPermissions("score:exchangeRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		exchangeRecordService.delete(id);
		addMessage(redirectAttributes, "删除积分对换记录成功");
		return "redirect:"+Global.ADMIN_PATH+"/score/exchangeRecord/?repage";
	}
	
	@RequiresPermissions("score:exchangeRecord:edit")
	@RequestMapping(value = "update")
	public String updateStatus(Long id,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String status = request.getParameter("status");
		exchangeRecordService.updateStatusById(id, status) ;
		addMessage(redirectAttributes, ("1".equals(status)) ? "核销":"作废"+"成功");
		return "redirect:"+Global.ADMIN_PATH+"/score/exchangeRecord/?repage";
	}
	
	

}
